package com.syni.mdd.yhd.common.all.listen.shopAssistant;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.entity.BmsBusiness;
import com.syni.mdd.yhd.common.all.entity.BmsUserBusiness;
import com.syni.mdd.yhd.common.all.entity.groupChat.BmsDxGroupChat;
import com.syni.mdd.yhd.common.all.entity.groupChat.BmsUserChat;
import com.syni.mdd.yhd.common.all.service.BmsBusinessService;
import com.syni.mdd.yhd.common.all.service.BmsDxGroupChatService;
import com.syni.mdd.yhd.common.all.service.BmsUserBusinessService;
import com.syni.mdd.yhd.common.all.service.groupChat.BmsUserChatService;
import com.syni.mdd.yhd.common.all.utils.SpecificationUtils;
import com.syni.mdd.yhd.common.all.utils.TokenUtils;
import com.syni.mdd.yhd.common.all.utils.aurora.AuroraMessageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @className AssistantHandle
 * @description TOO
 * @Author cfx
 * @DATE 2019/11/4 9:32
 * @VERSION 1.0
 **/
@Component
public class AssistantHandleListener implements ApplicationListener<AssistantEvent> {
    final String addAssistant = "addAssistant";
    final String deleteAssistant = "deleteAssistant";
    final String transferAssistant = "transferAssistant";

    @Autowired
    BmsDxGroupChatService bmsDxGroupChatService;

    @Autowired
    BmsUserChatService bmsUserChatService;

    @Autowired
    BmsBusinessService bmsBusinessService;

    @Autowired
    BmsUserBusinessService bmsUserBusinessService;
    @Override
    @Async
    public void onApplicationEvent(AssistantEvent assistantEvent) {
        String action = assistantEvent.getSource().toString();
        BmsUserBusiness bmsUserBusiness = assistantEvent.getBmsUserBusiness();
        BmsBusiness business = assistantEvent.getBmsBusiness();
        Integer businessId = assistantEvent.getBusinessId();
        Integer assistantId = assistantEvent.getAssistantId();
        Integer assistantRoleId = assistantEvent.getAssistantRoleId();
        Integer userId = assistantEvent.getUserId();
        String gid = assistantEvent.getGid();
        if(addAssistant.equals(action)){
            handAddAssistant(bmsUserBusiness,business);
        }else if(deleteAssistant.equals(action)){
            handDeleteAssistant(businessId,assistantId,assistantRoleId);
        }else if(transferAssistant.equals(action)){
            handTransferAssistant(businessId,userId,assistantId,gid);
        }

    }

    private void handTransferAssistant(Integer businessId,Integer userId,Integer assistantId,String gid) {

        BmsBusiness bmsBusiness = bmsBusinessService.getBusinessById(businessId);
        if(bmsBusiness != null) {
            if (gid != null) {
                //如果有群聊的情况下，管理员易主
                bmsUserChatService.transGroupChatManger(bmsBusiness, userId, assistantId);
                //更新一下店员呢称
                AuroraMessageUtils.updateBusinessUsers(bmsBusiness, assistantId, Constants.BUSINESS_ROLE_ID);
                //极光转移权限
                AuroraMessageUtils.transferGroupAdmin(gid, AuroraMessageUtils.USER_PREFIX + assistantId);
                //同时退出群聊
                bmsUserChatService.deleteGroupMember(businessId, userId, Constants.BUSINESS_ROLE_ID);
            }
            //极光删除这个用户
            AuroraMessageUtils.deleteUsers(userId, Constants.BUSINESS_ROLE_ID);
        }
        bmsUserBusinessService.assistantForceLogout(assistantId);
    }

    private void handDeleteAssistant(Integer businessId, Integer assistantId,Integer assistantRoleId) {
        bmsUserChatService.deleteGroupMember(businessId,assistantId,assistantRoleId);
        bmsUserBusinessService.assistantForceLogout(assistantId);
    }

    private void handAddAssistant(BmsUserBusiness bmsUserBusiness, BmsBusiness bmsBusiness) {
        //在有群的情况下，加入进群//在有群的情况下，加入进群
        List<BmsDxGroupChat> bmsDxGroupChats = bmsDxGroupChatService.getBaseDao().findByBusinessIdAndIsDelete(bmsUserBusiness.getBmsBusinessId(), Constants.COMMON_FLAG_FALSE_INT);
        if(bmsDxGroupChats != null && bmsDxGroupChats.size() > 0) {
            BmsDxGroupChat bmsDxGroupChat = bmsDxGroupChats.get(0);
            BmsUserChat bmsUserChat = new BmsUserChat();
            bmsUserChat.setUserId(bmsUserBusiness.getBmsUserId());
            bmsUserChat.setRoleId(Constants.ASSISTANT_ROLE_ID);
            bmsUserChat.setChatId(bmsDxGroupChat.getId());
            bmsUserChat.setBusinessId(bmsUserBusiness.getBmsBusinessId());
            bmsUserChat.setNewTime(new Date());
            bmsUserChat.setNameInGroupChat(bmsUserBusiness.getNameInStore());
            //bmsUserChat.setLoginName(bmsUserBusiness.getNameInStore());
            bmsUserChatService.getBaseDao().save(bmsUserChat);
            //同时注册极光
            List<BmsUserChat> bmsUserChats = new LinkedList<>();
            bmsUserChats.add(bmsUserChat);
            AuroraMessageUtils.registerBusinessUsers(bmsUserChats, bmsBusiness);
            //加入群
            AuroraMessageUtils.businessUserJoinGroup(bmsDxGroupChat.getGid(), bmsUserChat);
        }
        //刷新商户端的token
        bmsUserBusinessService.assistantForceLogout(bmsUserBusiness.getBmsUserId());
    }
}
